#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
FROM openanolis/anolisos:8.8

ENV LANG='en_US.UTF-8' LANGUAGE='en_US:en'

RUN yum install -y tzdata openssl curl ca-certificates fontconfig gzip tar \
    && yum clean all

COPY slim /usr/local/bin/slim

ENV EXTENDED_JAVA_VERSION 11.0.23.20+9

ENV STANDARD_JAVA_VERSION 11.0.23.19+9

ARG EDITION=extended

ARG SOURCE=github

RUN set -eux; \
    ARCH="$(objdump="$(command -v objdump)" && objdump --file-headers "$objdump" | awk -F '[:,]+[[:space:]]+' '$1 == "architecture" { print $2 }')"; \
    case "${ARCH}" in \
       aarch64|arm64) \
         if [ "${SOURCE}" = "github" ];then \
           if [ "${EDITION}" = "extended" ];then \
             ESUM='2f399231644fe1e3f1b4b5298e85f21f4863017767e9e5afb00ee46e2d7780d9'; \
             BINARY_URL='https://github.com/dragonwell-project/dragonwell11/releases/download/dragonwell-extended-11.0.23.20_jdk-11.0.23-ga/Alibaba_Dragonwell_Extended_11.0.23.20.9_aarch64_linux.tar.gz'; \
           else \
             ESUM='7541ec129a2f75bdbe8690db17ff4d61eb3ab8cd8b4933d7247bb90d7b162995'; \
             BINARY_URL='https://github.com/dragonwell-project/dragonwell11/releases/download/dragonwell-standard-11.0.23.19_jdk-11.0.23-ga/Alibaba_Dragonwell_Standard_11.0.23.19.9_aarch64_linux.tar.gz'; \
           fi \
         else \
           if [ "${EDITION}" = "extended" ];then \
             ESUM='2f399231644fe1e3f1b4b5298e85f21f4863017767e9e5afb00ee46e2d7780d9'; \
             BINARY_URL='https://dragonwell.oss-cn-shanghai.aliyuncs.com/11.0.23.20.9/Alibaba_Dragonwell_Extended_11.0.23.20.9_aarch64_linux.tar.gz'; \
           else \
             ESUM='7541ec129a2f75bdbe8690db17ff4d61eb3ab8cd8b4933d7247bb90d7b162995'; \
             BINARY_URL='https://dragonwell.oss-cn-shanghai.aliyuncs.com/11.0.23.19.9/Alibaba_Dragonwell_Standard_11.0.23.19.9_aarch64_linux.tar.gz'; \
           fi \
         fi \
         ;; \
       amd64|i386:x86-64) \
         if [ "${SOURCE}" = "github" ];then \
           if [ "${EDITION}" = "extended" ];then \
             ESUM='662dfdc584e21bcfb7ed87942b5bb4e71a7b7467d4c82211a3615d0834d1c833'; \
             BINARY_URL='https://github.com/dragonwell-project/dragonwell11/releases/download/dragonwell-extended-11.0.23.20_jdk-11.0.23-ga/Alibaba_Dragonwell_Extended_11.0.23.20.9_x64_linux.tar.gz'; \
           else \
             ESUM='87a5e476ee6115261e5face7cd081cd27719c8495bdc5b8b2e1304baeca5e9d9'; \
             BINARY_URL='https://github.com/dragonwell-project/dragonwell11/releases/download/dragonwell-standard-11.0.23.19_jdk-11.0.23-ga/Alibaba_Dragonwell_Standard_11.0.23.19.9_x64_linux.tar.gz'; \
           fi \
         else \
           if [ "${EDITION}" = "extended" ];then \
             ESUM='662dfdc584e21bcfb7ed87942b5bb4e71a7b7467d4c82211a3615d0834d1c833'; \
             BINARY_URL='https://dragonwell.oss-cn-shanghai.aliyuncs.com/11.0.23.20.9/Alibaba_Dragonwell_Extended_11.0.23.20.9_x64_linux.tar.gz'; \
           else \
             ESUM='87a5e476ee6115261e5face7cd081cd27719c8495bdc5b8b2e1304baeca5e9d9'; \
             BINARY_URL='https://dragonwell.oss-cn-shanghai.aliyuncs.com/11.0.23.19.9/Alibaba_Dragonwell_Standard_11.0.23.19.9_x64_linux.tar.gz'; \
           fi \
         fi \
         ;; \
       *) \
         echo "Unsupported arch: ${ARCH}"; \
         exit 1; \
         ;; \
    esac; \
    curl -so /tmp/openjdk.tar.gz ${BINARY_URL}; \
    echo "${ESUM} */tmp/openjdk.tar.gz" | sha256sum -c -; \
    mkdir -p /opt/java/openjdk; \
    cd /opt/java/openjdk; \
    tar -xf /tmp/openjdk.tar.gz --strip-components=1; \
    export PATH="/opt/java/openjdk/bin:$PATH"; \
    bash /usr/local/bin/slim/slim-java.sh /opt/java/openjdk; \
    rm -rf /tmp/openjdk.tar.gz /usr/local/bin/slim;

ENV JAVA_HOME=/opt/java/openjdk \
    PATH="/opt/java/openjdk/bin:$PATH"

RUN echo Verifying install ... \
    && echo javac --version && javac --version \
    && echo java --version && java --version \
    && echo Complete.

RUN PREFIX=$(echo ${EDITION} | awk '{print toupper($0)}') \
    && JAVA_VERSION=$(eval echo \$${PREFIX}_JAVA_VERSION) \
    && java -version 2>&1 | grep "${JAVA_VERSION}"

CMD ["jshell"]
